package com.bjesc_b2c.app.appraiset.mvp.view.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.base.BaseActivity;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.utils.CustomSharedPreferences;
import com.bjesc_b2c.app.appraiset.widget.dialog.DateTimeDialog;
import com.bjesc_b2c.app.appraiset.widget.dialog.DefeatCauseDialog;
import com.example.codeutils.utils.TimeUtils;
import com.rxretrofit_build.util.MapUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import rxretrofit_base.model.KeyBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by yidong on 2017/6/14.
 */

public class OrderInfoActivity extends BaseActivity implements DateTimeDialog.MyOnDateSetListener, DefeatCauseDialog.OnSelectListener {

    @Bind(R.id.tv_title)
    TextView title;
    @Bind(R.id.linear_1)
    LinearLayout linear_1;
    @Bind(R.id.linear_2)
    LinearLayout linear_2;
    @Bind(R.id.linear_3)
    LinearLayout linear_3;
    @Bind(R.id.linear_4)
    LinearLayout linear_4;
    @Bind(R.id.met_not_buy_remarks)
    EditText notBuyEdit;
    @Bind(R.id.not_met_remarks)
    EditText notMetEdit;
    @Bind(R.id.bojing_remarks)
    EditText bjEdit;
    @Bind(R.id.defeat_remarks)
    EditText defeatEdit;
    @Bind(R.id.ed_car_price)
    EditText carPriceEdit;
    @Bind(R.id.tv_car_defeat)
    TextView defeatTv;
    @Bind(R.id.tv_next_met_buy_time)
    TextView nextBuyTimeTv;
    @Bind(R.id.tv_next_met_time)
    TextView nextMetTimeTv;
    private int orderType;
    private String soldType;
    private long nextTime;
    private float soldPrice;
    private SimpleDateFormat mFormatter = new SimpleDateFormat("yyyy-MM-dd aa hh:mm");
    private List<View> viewList = new ArrayList<View>();

    @Override
    protected int getLayout() {
        return R.layout.activity_order_info;
    }

    @Override
    protected void initEventAndData() {
        title.setText("订单情况");
        viewList.add(linear_1);
        viewList.add(linear_2);
        viewList.add(linear_3);
        viewList.add(linear_4);
    }

    @Override
    protected void createPresenter() {

    }

    @OnClick({R.id.tv_type_one, R.id.tv_type_two, R.id.tv_type_three, R.id.tv_type_four, R.id.btn_post,
            R.id.rlty_not_met_time, R.id.rlty_met_not_buy, R.id.rlty_defeat, R.id.iv_return})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.iv_return:
                finish();
            case R.id.tv_type_one:
                setViewVisible(linear_1);
                nextBuyTimeTv.setText("");
                nextMetTimeTv.setText("");
                nextTime = 0;
                orderType = 4;
                break;
            case R.id.tv_type_two:
                setViewVisible(linear_2);
                nextBuyTimeTv.setText("");
                nextMetTimeTv.setText("");
                nextTime = 0;
                orderType = 5;
                break;
            case R.id.tv_type_three:
                setViewVisible(linear_3);
                orderType = 2;
                break;
            case R.id.tv_type_four:
                setViewVisible(linear_4);
                orderType = 3;
                break;
            case R.id.rlty_not_met_time:
            case R.id.rlty_met_not_buy:
                DateTimeDialog dateTime = new DateTimeDialog(this, null, this);
                dateTime.hideOrShow();
                break;
            case R.id.rlty_defeat:
                DefeatCauseDialog dialog = new DefeatCauseDialog(this, this);
                dialog.show();
                break;
            case R.id.btn_post:
                postData();
                break;
        }
    }

    @Override
    public void onDateSet(Date date) {
        nextBuyTimeTv.setText(mFormatter.format(date));
        nextMetTimeTv.setText(mFormatter.format(date));
        nextTime = TimeUtils.date2Millis(date);
    }

    @Override
    public void onSelect(String typeId, String name) {
        defeatTv.setText(name);
        soldType = typeId;
    }

    //    dispatch_id	是	int	派单id
//    status	是	int	需要设置状态。只允许传入 2博精成交（终止状态） 3战败（终止状态） 4未见面 5见面未成交
//    desc	是	int	备注（status状态为5、必填）
//    time	是	int	下次跟进时间戳 （如果status状态为4，5，则必填）
//    soldPrice	否	float	售价（status状态为2，必填）
//    soldType	否	int	战败类型（1，优信成交；2，看车成交；3，车商成交；）
    private void postData() {
        String desc = "";
        HashMap<String, Object> build = new MapUtil()
                .put("dispatch_id", CustomSharedPreferences.getString(this, KeyBean.ORDER_ID.valueOf(), KeyBean.FILE_NAME.valueOf())).build();
        switch (orderType) {
            case 2:
                desc = bjEdit.getText().toString();
                if (!TextUtils.isEmpty(carPriceEdit.getText().toString())) {
                    soldPrice = Float.valueOf(carPriceEdit.getText().toString());
                }
                build.put("status", orderType);
                build.put("soldPrice", soldPrice);
                build.put("desc", desc);
                break;
            case 3:
                desc = defeatEdit.getText().toString();
                build.put("status", orderType);
                build.put("soldType", soldType);
                build.put("desc", desc);
                break;
            case 4:
                desc = notMetEdit.getText().toString();
                build.put("status", orderType);
                build.put("time", nextTime);
                build.put("desc", desc);
                break;
            case 5:
                desc = notBuyEdit.getText().toString();
                build.put("status", orderType);
                build.put("time", nextTime);
                build.put("desc", desc);
                break;
        }

        RxRequest.request(ShowType.SHOW, RxRequest.getClient(OrderInfoActivity.this, build).create(IApiActivityRequest.class).shutDownDispatch(),
                new BaseSubscriber<TokenBean>(OrderInfoActivity.this) {
                    @Override
                    public void onSuccess(TokenBean o) {
//                        showToast(o.getMessage());
                        if (o.getMessage().equals("0")){
                            setResult(11);
                            finish();
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }

    private void setViewVisible(View view) {
        for (int i = 0; i < viewList.size(); i++) {
            if (viewList.get(i) == view) {
                viewList.get(i).setVisibility(View.VISIBLE);
            } else {
                viewList.get(i).setVisibility(View.GONE);
            }
        }
    }
}
